package com.retailshop.shopuser.WapUserController;

import com.alibaba.fastjson.JSONObject;
import com.retailshop.shopuser.common.*;
import com.retailshop.shopuser.entity.HttpRequestConfig;
import com.retailshop.shopuser.entity.StoreConcern;
import com.retailshop.shopuser.service.StoreConcernService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RequestMapping("/wap")
@Controller
public class StoreConcernController {

    @Autowired
    StoreConcernService storeConcernService;
    @Autowired
    HttpRequestConfig httpRequestConfig;

    /**
     * 关注店铺
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/addConcernStore")
    public Response addConcernStore(HttpServletRequest request) {
        String storeIds = request.getParameter("storeId");
        String token = request.getHeader("token");
        int userNoId = 0;
        if (storeIds == null || storeIds == "" || TokenUtils.getUserNoId(token) == 0) {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        int storeId = 0;
        storeId = Integer.parseInt(storeIds);
        StoreConcern storeConcern1 = storeConcernService.concernStore(storeId);//查看是否关注过
        if (storeConcern1 != null) {
            return new Response(MessageConstants.CONERN, null, 0, "", request);
        }
        userNoId = TokenUtils.getUserNoId(token);
        StoreConcern storeConcern = new StoreConcern();
        storeConcern.setStoreId(storeId);
        storeConcern.setUserNoId(userNoId);
        storeConcern.setConcernState("1");
        int row = storeConcernService.insertSelective(storeConcern);
        if (row > 0) {
            //修改该店铺的关注数量


//            String url = "http://192.168.0.133:8086/wap/updateConcern";
            String url = HttpRequestConfig.orderServerUrl + "/wap/updateConcern";
            String storeId1 = "storeId=" + storeId;
            String result = HttpRequest.sendGet(url, storeId1);

            JSONObject jsonobject = JSONObject.parseObject(result);
            String code = jsonobject.get("code").toString();
            if ("000000".equalsIgnoreCase(code)) {
                return new Response(MessageConstants.MSG_SUCCESS, row, 0, "", request);
            } else {
                return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
            }

        } else {
            return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
        }
    }


    /**
     * 获取关注的店铺ID
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getConernStoreId")
    public Response getConernStoreID(HttpServletRequest request) {
        String userNoIds = request.getParameter("userNoId");
        System.out.println("userNoId=" + userNoIds);
        if (userNoIds == null || "".equalsIgnoreCase(userNoIds)) {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        int userNoId = 0;
        if (StringUtil.isNumeric(userNoIds)) {
            userNoId = Integer.parseInt(userNoIds);
        }

        List<StoreConcern> storeConcerns = storeConcernService.getConcernStoresByUserNoId(userNoId);
        return new Response(MessageConstants.MSG_SUCCESS, storeConcerns, 0, "", request);
    }


}
